package home.contaduria.dao.jpa;

import home.contaduria.dao.ExpenseTypeDao;
import home.contaduria.domain.Client;
import home.contaduria.domain.ExpenseType;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Repository;

@Repository("expenseTypeDaoJpaImpl")
public class ExpenseTypeDaoJpaImpl extends GenericDaoJpaImpl<ExpenseType, Long> implements ExpenseTypeDao {
    private static final Logger logger = LoggerFactory.getLogger(ExpenseTypeDaoJpaImpl.class);

    @Override
    public Collection<ExpenseType> loadClientExpenseTypes(Client client) {
        return getEntityManager().createNamedQuery("ExpenseType.loadClientExpenseTypes", ExpenseType.class).setParameter("client", client)
                .getResultList();
    }

    @Override
    public ExpenseType findExpenseType(String name, Client client) {
        logger.info("Searching expense type with name {} for client {}", name, client);
        ExpenseType singleResult = DataAccessUtils.singleResult(getEntityManager()
                .createNamedQuery("ExpenseType.findExpenseType", ExpenseType.class).setParameter("client", client)
                .setParameter("name", name).getResultList());
        logger.info("Found expense type: {}", singleResult);
        return singleResult;
    }

}
